package com.iot08.admin.tools;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;

/**
 * DES加密解密算法 (嵌套Base64)
 * @author weichao 1299101858@qq.com
 * @version 1.0
 * @date 3/29/22 6:08 PM
 */
@Slf4j
public class DESEncryptTools {

    // 密钥 长度不得小于24
    private final static String secretKey = "jianyou-watch-DES-secretKey" ;
    // 向量 可有可无 终端后台也要约定
    private final static String iv = "75395128";
    // 加解密统一使用的编码方式
    private final static String encoding = "utf-8";

    /**
     * 3DES加密
     *
     * @param plainText
     *            普通文本
     * @return
     * @throws Exception
     */
    public static String encode(String plainText) {
        if (plainText == null){
            return null;
        }
        try{
            Key deskey = null;
            DESedeKeySpec spec = new DESedeKeySpec(secretKey .getBytes());
            SecretKeyFactory keyfactory = SecretKeyFactory.getInstance( "desede");
            deskey = keyfactory.generateSecret( spec);

            Cipher cipher = Cipher.getInstance( "desede/CBC/PKCS5Padding");
            IvParameterSpec ips = new IvParameterSpec( iv.getBytes());
            cipher.init(Cipher. ENCRYPT_MODE, deskey, ips);
            byte[] encryptData = cipher.doFinal(plainText.getBytes( encoding));
//        return base64Encoder(new String(encryptData, encoding));

            //Base64 base64 = new Base64(false);
            //base64.encodeAsString(encryptData);
            return Base64.encodeBase64String(encryptData);
        }catch (Exception error){
            log.error("加密失败 plainText:"+plainText, error);
            return null;
        }

    }

    /**
     * 3DES解密
     *
     * @param encryptText
     *            加密文本
     * @return
     * @throws Exception
     */
    public static String decode(String encryptText){
        if (encryptText == null){
            return null;
        }
        try {
            Key deskey = null;
            DESedeKeySpec spec = new DESedeKeySpec( secretKey.getBytes());
            SecretKeyFactory keyfactory = SecretKeyFactory.getInstance( "desede");
            deskey = keyfactory. generateSecret( spec);
            Cipher cipher = Cipher.getInstance( "desede/CBC/PKCS5Padding" );
            IvParameterSpec ips = new IvParameterSpec( iv.getBytes());
            cipher. init(Cipher. DECRYPT_MODE, deskey, ips);

            //Base64解码 后再解密
            Base64 base64 = new Base64(false);
            byte[] decryptData = cipher. doFinal(base64.decode(encryptText));
            return new String(decryptData, encoding);
        }catch (Exception error){
            log.error("解密失败 encryptText:"+encryptText, error);
            return null;
        }

    }

    public static void main(String[] args) throws Exception{
        String msg = "pinxuan2024";
        System.out.println("加密前："+msg);
        String encrypt = DESEncryptTools.encode(msg);
        System.out.println("加密后："+ encrypt);
////        byte[] deMsgBytes = DESEncryptTools.decode(encryptBytes);
////        String deMsg = DESEncryptTools.decode(encrypt);
////        System.out.println("解密后："+deMsg);
//
        String testEncrypt = "VwJZDNNO9gVWmrjRV/eMow==";
        testEncrypt = "Zd29RGVqNoGXhTwt6/fSDg==";
        testEncrypt = "B+3eL7QsTVZAw3YMVvJJWg==";
        String testEncrypt_decodeStr = DESEncryptTools.decode(testEncrypt);
        System.out.println("解密前: testEncrypt:"+testEncrypt+" 解密后：testEncrypt_decodeStr:"+testEncrypt_decodeStr);
    }

}
